package com.hsingh.eclipse.mercury.ui;

import java.util.Comparator;
import java.util.List;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.dialogs.FilteredItemsSelectionDialog;

import com.hsingh.eclipse.mercury.TaskComparator;
import com.hsingh.eclipse.mercury.TaskLabelProvider;
import com.hsingh.eclipse.mercury.extensionpoint.definition.ITask;
import com.hsingh.eclipse.mercury.handlers.Activator;

public class MercuryDialog extends FilteredItemsSelectionDialog {

  private final List<ITask> tasks;
  private final ExecutionEvent executionEvent;

  public MercuryDialog(Shell shell, List<ITask> tasks, ExecutionEvent executionEvent) {
    super(shell);
    this.tasks = tasks;
    this.executionEvent = executionEvent;

    setListLabelProvider(new TaskLabelProvider(false));
    setDetailsLabelProvider(new TaskLabelProvider(true));
    setSelectionHistory(new SearchHistory());
  }

  @Override
  protected Control createExtendedContentArea(Composite container) {
    return null;
  }

  @Override
  protected ItemsFilter createFilter() {
    return new SearchFilter();
  }

  @Override
  protected void okPressed() {
    StructuredSelection selectedItems = getSelectedItems();
    if (selectedItems != null && selectedItems.getFirstElement() != null) {
      ITask task = (ITask) selectedItems.getFirstElement();
      try {
        super.okPressed();
        task.execute(executionEvent);

      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

  @Override
  protected void fillContentProvider(AbstractContentProvider contentProvider, ItemsFilter itemsFilter,
      IProgressMonitor progressMonitor) throws CoreException {
    progressMonitor.beginTask("Looking for tasks...", tasks.size());
    for (ITask task : tasks) {
      contentProvider.add(task, itemsFilter);
      progressMonitor.worked(1);
    }
  }

  private static final String SETTINGS = MercuryDialog.class.getCanonicalName();

  @Override
  protected IDialogSettings getDialogSettings() {
    IDialogSettings settings = Activator.getDefault().getDialogSettings().getSection(SETTINGS);
    if (settings == null) {
      settings = Activator.getDefault().getDialogSettings().addNewSection(SETTINGS);
    }
    return settings;

  }

  @Override
  public String getElementName(Object task) {
    return ((ITask) task).getName();
  }

  @Override
  protected Comparator<ITask> getItemsComparator() {
    return new TaskComparator();
  }

  @Override
  protected IStatus validateItem(Object task) {
    return Status.OK_STATUS;
  }

  class SearchFilter extends ItemsFilter {

    @Override
    public boolean isConsistentItem(Object item) {
      return true;
    }

    @Override
    public boolean matchItem(Object item) {
      return matches(((ITask) item).getName());
    }
  }

  class SearchHistory extends SelectionHistory {

    @Override
    protected Object restoreItemFromMemento(IMemento element) {
      return null;
    }

    @Override
    protected void storeItemToMemento(Object obj, IMemento element) {}
  }
}
